/*
 * PanelAffine.java
 *
 * Created on 2008年4月6日, 下午4:21
 */

package GUI;

import Manager.Manager;
import Global.*;
/**
 *
 * @author  littlechild
 */
public class PanelAffine extends javax.swing.JPanel {
    Manager manager;
    PanelCavas drawcavas;
    boolean isInitiating;
    //define the scale for the sliders
    static final double scale=100;
    static final int maxS=300;
    static final int minS=1;
    static final int maxT=300;
    static final int minT=1;
    static final int maxU=100;
    static final int minU=-100;
    static final int maxThta=180;
    static final int minThta=-180;
    /** Creates new form PanelAffine */
    public PanelAffine(Manager mg,PanelCavas pc) {
        manager=mg;
        drawcavas=pc;
        initComponents();
        init();
    }

    private void init() {
        isInitiating=true;
        this.jSliderS.setValue((int)scale);
        this.jSliderT.setValue((int)scale);
        this.jSliderThta.setValue(0);
        this.jSliderU.setValue(0);
        this.jSliderS.setMinimum(minS);
        this.jSliderS.setMaximum(maxS);
        this.jSliderT.setMinimum(minT);
        this.jSliderT.setMaximum(maxT);
        this.jSliderThta.setMinimum(minThta);
        this.jSliderThta.setMaximum(maxThta);
        this.jSliderU.setMaximum(maxU);
        this.jSliderU.setMinimum(minU);
        isInitiating=false;
    }
    
    private double slidev2realv(int sliderValue) {
        return sliderValue/scale;
    }
    
    private void preview() {
        if(!isInitiating) {
            String paras[]=new String[]{
                String.valueOf(slidev2realv(this.jSliderS.getValue())),
                String.valueOf(slidev2realv(this.jSliderT.getValue())),
                String.valueOf(slidev2realv(this.jSliderU.getValue())),
                String.valueOf(this.jSliderThta.getValue())}; //notice Thta do not have scaling
            this.drawcavas.setImage(
                    manager.operate(Global.opAffine, paras,true));
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSliderS = new javax.swing.JSlider();
        jSliderT = new javax.swing.JSlider();
        jSliderU = new javax.swing.JSlider();
        jSliderThta = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        labelS = new javax.swing.JLabel();
        labelT = new javax.swing.JLabel();
        labelU = new javax.swing.JLabel();
        labelThta = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jSliderS.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderSMouseReleased(evt);
            }
        });
        jSliderS.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderSStateChanged(evt);
            }
        });

        jSliderT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderTMouseReleased(evt);
            }
        });
        jSliderT.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderTStateChanged(evt);
            }
        });

        jSliderU.setMinimum(-100);
        jSliderU.setValue(0);
        jSliderU.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderUMouseReleased(evt);
            }
        });
        jSliderU.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderUStateChanged(evt);
            }
        });

        jSliderThta.setMaximum(180);
        jSliderThta.setMinimum(-180);
        jSliderThta.setValue(0);
        jSliderThta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderThtaMouseReleased(evt);
            }
        });
        jSliderThta.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderThtaStateChanged(evt);
            }
        });

        jLabel1.setText("尺度：s=");

        jLabel2.setText("伸缩：t=");

        jLabel3.setText("扭曲：u=");

        jLabel4.setText("旋转：Theta=");

        jButton1.setText("确定");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("取消");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelThta)
                            .addComponent(labelU)
                            .addComponent(labelT)
                            .addComponent(labelS)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSliderT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSliderU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSliderThta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSliderS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addComponent(jButton1)
                        .addGap(35, 35, 35)
                        .addComponent(jButton2)))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(labelS))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(labelT))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(labelU))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(labelThta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderThta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jSliderSStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderSStateChanged
        // TODO add your handling code here:
        this.labelS.setText(String.valueOf(slidev2realv(this.jSliderS.getValue())));
    }//GEN-LAST:event_jSliderSStateChanged

    private void jSliderTStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderTStateChanged
        // TODO add your handling code here:
        this.labelT.setText(String.valueOf(slidev2realv(this.jSliderT.getValue())));
    }//GEN-LAST:event_jSliderTStateChanged

    private void jSliderUStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderUStateChanged
        // TODO add your handling code here:
        this.labelU.setText(String.valueOf(slidev2realv(this.jSliderU.getValue())));
    }//GEN-LAST:event_jSliderUStateChanged

    private void jSliderThtaStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderThtaStateChanged
        // TODO add your handling code here:
        this.labelThta.setText(String.valueOf(this.jSliderThta.getValue()));
    }//GEN-LAST:event_jSliderThtaStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String paras[]=new String[]{
            String.valueOf(slidev2realv(this.jSliderS.getValue())),
            String.valueOf(slidev2realv(this.jSliderT.getValue())),
            String.valueOf(slidev2realv(this.jSliderU.getValue())),
            String.valueOf(this.jSliderThta.getValue())}; //notice Thta do not have scaling
        this.drawcavas.setImage(
                manager.operate(Global.opAffine, paras));
        CommonFunc.disposeFrame(this);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.drawcavas.setImage(manager.operate(Global.opNone, null));
        CommonFunc.disposeFrame(this);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jSliderSMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderSMouseReleased
        // TODO add your handling code here:
        preview();
    }//GEN-LAST:event_jSliderSMouseReleased

    private void jSliderTMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderTMouseReleased
        // TODO add your handling code here:
        preview();
    }//GEN-LAST:event_jSliderTMouseReleased

    private void jSliderUMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderUMouseReleased
        // TODO add your handling code here:
        preview();
    }//GEN-LAST:event_jSliderUMouseReleased

    private void jSliderThtaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderThtaMouseReleased
        // TODO add your handling code here:
        preview();
    }//GEN-LAST:event_jSliderThtaMouseReleased
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSlider jSliderS;
    private javax.swing.JSlider jSliderT;
    private javax.swing.JSlider jSliderThta;
    private javax.swing.JSlider jSliderU;
    private javax.swing.JLabel labelS;
    private javax.swing.JLabel labelT;
    private javax.swing.JLabel labelThta;
    private javax.swing.JLabel labelU;
    // End of variables declaration//GEN-END:variables
    
}
